package com.lll.protocol;

import lombok.Data;

import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 自定义传输的报文格式
 */
@Data
public class Message<T> implements Serializable {

    private static final long serialVersionUID = 7112804076022372113L;
    /**
     * 消息头
     */
    private MessageHeader header;

    /**
     * 消息体 LoginRequestMessage.....
     */
    private T body;


    /* 存储message body类型 方便解码的时候反序列化 暂时没有更优雅的方式*****/
    /********报文类型定义开始***/
    public static final int LoginRequestMessage = 0;
    public static final int LoginResponseMessage = 1;
    public static final int ChatRequestMessage = 2;
    public static final int ChatResponseMessage = 3;
    public static final int GroupCreateRequestMessage = 4;
    public static final int GroupCreateResponseMessage = 5;
    public static final int GroupJoinRequestMessage = 6;
    public static final int GroupJoinResponseMessage = 7;
    public static final int GroupQuitRequestMessage = 8;
    public static final int GroupQuitResponseMessage = 9;
    public static final int GroupChatRequestMessage = 10;
    public static final int GroupChatResponseMessage = 11;
    public static final int GroupMembersRequestMessage = 12;
    public static final int GroupMembersResponseMessage = 13;

    public static final int PingMessage = 14;

    public static final int PongMessage = 15;
    /********报文类型定义结束***/
    private static final ConcurrentHashMap<Integer, Class> messageClasses = new ConcurrentHashMap<>(256);

    static {
        messageClasses.put(LoginRequestMessage, LoginRequestMessage.class);
        messageClasses.put(LoginResponseMessage, LoginResponseMessage.class);
        messageClasses.put(ChatRequestMessage, ChatRequestMessage.class);
        messageClasses.put(ChatResponseMessage, ChatResponseMessage.class);
        messageClasses.put(GroupCreateRequestMessage, GroupCreateRequestMessage.class);
        messageClasses.put(GroupCreateResponseMessage, GroupCreateResponseMessage.class);
        messageClasses.put(GroupJoinRequestMessage, GroupJoinRequestMessage.class);
        messageClasses.put(GroupJoinResponseMessage, GroupJoinResponseMessage.class);
        messageClasses.put(GroupQuitRequestMessage, GroupQuitRequestMessage.class);
        messageClasses.put(GroupQuitResponseMessage, GroupQuitResponseMessage.class);
        messageClasses.put(GroupChatRequestMessage, GroupChatRequestMessage.class);
        messageClasses.put(GroupChatResponseMessage, GroupChatResponseMessage.class);
        messageClasses.put(GroupMembersRequestMessage, GroupMembersRequestMessage.class);
        messageClasses.put(GroupMembersResponseMessage, GroupMembersResponseMessage.class);
    }

    public static Class getMessageClass(int messageType) {
        return messageClasses.get(messageType);
    }

    /* 存储message body类型*****/

}
